import {invoke} from "@tauri-apps/api/core";
import {storeToRefs} from "pinia";
import {useUserMessageStore} from "../stores/user_message.ts";

export function loadMessage() {
    const {messageMap} = storeToRefs(useUserMessageStore());
    const decoder = new TextDecoder('utf-8');
    invoke("query_messages", {}).then((res) => {
        console.log("query_messages");
        let params = {};
        for (let key in res) {
            let msgs: object[] = res[key];
            let msgList = [];
            for (let i = 0; i < msgs.length; i++) {
                let msg = {
                    sender_account: msgs[i].sender_account,
                    to_account: msgs[i].to_account,
                    message_type: msgs[i].message_type,
                    content: decoder.decode(new Uint8Array((msgs[i].content))),
                }
                msgList.push(msg);
            }
            params[key] = msgList;
        }
        messageMap.value = params;
    });
}